package servlet;


import cl.ccs.migracionbcmc.dao.FirmasDao;
import cl.ccs.migracionbcmc.dao.factory.FirmasDaoFactory;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ImageServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "image/jpg; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idFirmante = "";
        try {
            idFirmante = request.getParameter("idFirmante");


            FirmasDao firmasDao = FirmasDaoFactory.getInstance();
            byte[] bytesImage = firmasDao.getImageFirmante(Long.parseLong(idFirmante));
            response.setContentType(CONTENT_TYPE);
            ServletOutputStream op = response.getOutputStream();
            for (int i = 0; i < bytesImage.length; i++) {
                op.write(bytesImage[i]);
            }

            op.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
